﻿using smforj.common.model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace smforj.common.dao
{
    /// <summary>
    /// 衣柜数据操作dao
    /// </summary>
    public interface IWardRobeDao
    {
        /// <summary>
        /// 批量保存数据
        /// </summary>
        /// <param name="list">WardRole数据</param>
        /// <returns>0 成功 其他失败</returns>
        int SaveWardRoleBatch(List<WardRobe> list);


        /// <summary>
        /// 批量保存数据
        /// </summary>
        /// <param name="list">WardRole数据</param>
        /// <returns>0 成功 其他失败</returns>
        int SavePreferenceShowBatch(List<PreferenceShow> list);

        /// <summary>
        /// 查询所有的喜好信息
        /// </summary>
        /// <returns></returns>
        List<PreferenceShow> GetPreferenceShows(IQuery query);

        /// <summary>
        /// 查询所有的喜好信息
        /// </summary>
        /// <returns></returns>
        List<Preference> GetPreferences(IQuery query);

        /// <summary>
        /// 上传数据到远程服务器
        /// </summary>
        /// <returns></returns>
        int UploadToRomote();

        /// <summary>
        /// 根据id删除本地数据
        /// </summary>
        /// <param name="id"></param>
        int DeleteById(String id);
    }


    public class WardRobeSqliteDaoImpl : IWardRobeDao
    {
        //private static Mforj.Common.Sqlite.SqliteDB sqldb = null;

        //public WardRobeSqliteDaoImpl()
        //{ 
        //    init();
        //}

        //private void init()
        //{
        //    if (sqldb == null)
        //        sqldb = Mforj.Common.Sqlite.SQLiteHelper.GetInstance();
        //}

        //public int SaveWardRoleBatch(List<WardRobe> list)
        //{
        //    if (list == null || list.Count < 1)
        //        return -1;
        //    DbProviderFactory factory = SQLiteFactory.Instance;
        //    using (DbConnection conn = factory.CreateConnection())
        //    {
        //        DbTransaction trans = null;
        //        DbCommand cmd = null;
        //        try
        //        {
        //            // 连接数据库  
        //            conn.ConnectionString = sqldb.ConnectionString;
        //            conn.Open();
        //            trans = conn.BeginTransaction();
        //            String sql = "";
        //            cmd = conn.CreateCommand();
        //            cmd.Connection = conn; 
        //            //保存新的数据
        //            foreach (WardRobe model in list)
        //            {
        //                sql = "INSERT INTO `preference` (`id`, `rfid`, `status`, `is_deleted`, `hold_time`, `location`, `remark`, `update_date`, `create_date`)" +
        //                    " VALUES (@id, @rfid, @status, @is_deleted,  @hold_time, @location, @remark, @update_date,@create_date);";
        //                SQLiteParameter[] parameters = {
        //                new SQLiteParameter("@id", DbType.String), 
        //                new SQLiteParameter("@rfid", DbType.String), 
        //                new SQLiteParameter("@status", DbType.Int32,4),
        //                new SQLiteParameter("@is_deleted", DbType.Int32,4),
        //                new SQLiteParameter("@hold_time", DbType.Int32,4), 
        //                new SQLiteParameter("@location", DbType.String),
        //                new SQLiteParameter("@remark", DbType.String),
        //                new SQLiteParameter("@update_date", DbType.DateTime),
        //                new SQLiteParameter("@create_date", DbType.DateTime) 
        //                };
        //                DateTime date = DateTime.Now;
        //                parameters[0].Value = Guid.NewGuid().ToString();
        //                parameters[1].Value = model.RFID;
        //                parameters[2].Value = 1;
        //                parameters[3].Value = 0;
        //                parameters[4].Value = model.Holdtime;
        //                parameters[5].Value = model.Loaction;
        //                parameters[6].Value = model.ReaderIdentity;
        //                parameters[7].Value = date;
        //                parameters[8].Value = date; 
        //                cmd.CommandText = sql;
        //                cmd.Parameters.AddRange(parameters);
        //                cmd.ExecuteNonQuery();
        //            }
        //            trans.Commit();
        //            return 0;
        //        }
        //        catch (Exception ex)
        //        { 
        //            trans.Rollback();
        //            return -1;
        //        }
        //        finally
        //        {
        //            cmd.Dispose();
        //            conn.Close();
        //        }
        //    }
        //}

        //public int UploadToRomote()
        //{
        //    throw new NotImplementedException();
        //}

        //public int DeleteById(string id)
        //{
        //    int iRes = -1;
        //    try
        //    {
        //        String sql = String.Format("DELETE FROM `preference` where id ='{0}'", id);
        //        iRes = sqldb.ExecuteNonQueryTrans(CommandType.Text, sql);
        //    }
        //    catch (Exception ex)
        //    { 
        //        iRes = -1;
        //    }
        //    return iRes;
        //}

        ///// <summary>
        ///// 查询所有的喜好信息
        ///// </summary>
        ///// <returns></returns>
        //public List<Preference> GetPreferences(IQuery query)
        //{
        //    List<Preference> list = new List<Preference>();
        //    if (null == query)
        //        return list;
        //    try
        //    {
        //        string sql = "";
        //        if (!String.IsNullOrEmpty(query.GetID()))
        //            sql = "SELECT `id`, `rfid`, `status`, `is_deleted`, `hold_time`, `location`, `remark`, `update_date`, `create_date` FROM preference where id ='" + query.GetID() + "' ";
        //        else
        //            sql = "SELECT `id`, `rfid`, `status`, `is_deleted`, `hold_time`, `location`, `remark`, `update_date`, `create_date` FROM preference ";

        //        DataTable dt = sqldb.ExecuteDataTable(CommandType.Text, sql.ToString());
        //        if (null != dt && dt.Rows.Count > 0)
        //        {
        //            Preference model = null;
        //            foreach (DataRow row in dt.Rows)
        //            {
        //                model = new Preference();
        //                model.id = Convert.ToString(row["id"]);
        //                model.rfid = Convert.ToString(row["rfid"]); 
        //                model.status = Convert.ToInt32(row["status"]);
        //                model.is_deleted = Convert.ToInt32(row["is_deleted"]);
        //                model.holdtime = Convert.ToInt32(row["hold_time"]);
        //                model.location = Convert.ToString(row["location"]); 
        //                model.remark = Convert.ToString(row["remark"]);
        //                model.create_date = Convert.ToDateTime(row["create_date"]);
        //                model.update_date = Convert.ToDateTime(row["update_date"]);


        //                list.Add(model);
        //            }
        //        }

        //        return list;
        //    }
        //    catch (Exception ex)
        //    { 
        //        list.Clear();
        //        return list;
        //    }
        //}


        //public int SavePreferenceShowBatch(List<PreferenceShow> list)
        //{
        //    if (list == null || list.Count < 1)
        //        return -1;
        //    DbProviderFactory factory = SQLiteFactory.Instance;
        //    using (DbConnection conn = factory.CreateConnection())
        //    {
        //        DbTransaction trans = null;
        //        DbCommand cmd = null;
        //        try
        //        {
        //            // 连接数据库  
        //            conn.ConnectionString = sqldb.ConnectionString;
        //            conn.Open();
        //            trans = conn.BeginTransaction();
        //            String sql = "";
        //            cmd = conn.CreateCommand();
        //            cmd.Connection = conn;

        //            sql = String.Format("DELETE FROM `preference_show` where location='{0}' ", list[0].location);
        //            cmd.CommandText = sql;
        //            cmd.ExecuteNonQuery();
        //            //保存新的数据
        //            foreach (PreferenceShow model in list)
        //            {
        //                sql = "INSERT INTO `preference_show` (`id`, `rfid`, `image`, `type`, `holdtime`, `location`, `remark`, `count`)" +
        //                    " VALUES (@id, @rfid, @image, @type,  @holdtime, @location, @remark, @count);";
        //                SQLiteParameter[] parameters = {
        //                new SQLiteParameter("@id", DbType.String), 
        //                new SQLiteParameter("@rfid", DbType.String), 
        //                new SQLiteParameter("@image", DbType.String),
        //                new SQLiteParameter("@type", DbType.String), 
        //                new SQLiteParameter("@holdtime", DbType.Int32,4), 
        //                new SQLiteParameter("@location", DbType.String),
        //                new SQLiteParameter("@remark", DbType.String),
        //                new SQLiteParameter("@count", DbType.Int32,4)
        //                };
        //                DateTime date = DateTime.Now;
        //                parameters[0].Value = model.id;
        //                parameters[1].Value = model.rfid;
        //                parameters[2].Value = model.image;
        //                parameters[3].Value = model.type;
        //                parameters[4].Value = model.holdtime;
        //                parameters[5].Value = model.location;
        //                parameters[6].Value = model.remark;
        //                parameters[7].Value = model.Count ; 
        //                cmd.CommandText = sql;
        //                cmd.Parameters.AddRange(parameters);
        //                cmd.ExecuteNonQuery();
        //            }
        //            trans.Commit();
        //            return 0;
        //        }
        //        catch (Exception ex)
        //        {
        //            trans.Rollback();
        //            return -1;
        //        }
        //        finally
        //        {
        //            cmd.Dispose();
        //            conn.Close();
        //        }
        //    }
        //}

        //public List<PreferenceShow> GetPreferenceShows(IQuery query)
        //{
        //    List<PreferenceShow> list = new List<PreferenceShow>();
        //    if (null == query)
        //        return list;
        //    try
        //    {
        //        string sql = "";
        //        if (!String.IsNullOrEmpty(query.GetID()))
        //            sql = "SELECT `id`, `rfid`, `image`, `type`, `holdtime`, `location`, `remark`, `count` FROM preference_show where location ='" + query.GetID() + "' ";
        //        else
        //            sql = "SELECT `id`, `rfid`, `image`, `type`, `holdtime`, `location`, `remark`, `count` FROM preference_show ";

        //        DataTable dt = sqldb.ExecuteDataTable(CommandType.Text, sql.ToString());
        //        if (null != dt && dt.Rows.Count > 0)
        //        {
        //            PreferenceShow model = null;
        //            foreach (DataRow row in dt.Rows)
        //            {
        //                model = new PreferenceShow();
        //                model.id = Convert.ToString(row["id"]);
        //                model.rfid = Convert.ToString(row["rfid"]);
        //                model.image = Convert.ToString(row["image"]);
        //                model.Count = Convert.ToInt32(row["count"]);
        //                model.holdtime = Convert.ToInt32(row["holdtime"]);
        //                model.location = Convert.ToString(row["location"]);
        //                model.remark = Convert.ToString(row["remark"]); 


        //                list.Add(model);
        //            }
        //        }

        //        return list;
        //    }
        //    catch (Exception ex)
        //    {
        //        list.Clear();
        //        return list;
        //    }
        //}
        public int SaveWardRoleBatch(List<WardRobe> list)
        {
            throw new NotImplementedException();
        }

        public int SavePreferenceShowBatch(List<PreferenceShow> list)
        {
            throw new NotImplementedException();
        }

        public List<PreferenceShow> GetPreferenceShows(IQuery query)
        {
            throw new NotImplementedException();
        }

        public List<Preference> GetPreferences(IQuery query)
        {
            throw new NotImplementedException();
        }

        public int UploadToRomote()
        {
            throw new NotImplementedException();
        }

        public int DeleteById(string id)
        {
            throw new NotImplementedException();
        }
    }
}
